#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2024/11/13 16:04
# @Author : XXX
# @Site : 
# @File : control_temp.py
# @Software: PyCharm

import torch
from train_all_offline import Model
from util.loguru_util import myLogger

from collections import deque
import pandas as pd
from all_config import state_dict_path, model


# 创建模型实例


# 选择设备
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)

if 1:
    # 加载模型的状态字典
    state_dict = torch.load(state_dict_path, map_location=device)

    # 将状态字典加载到模型中
    model.load_state_dict(state_dict)

    # 设置模型为评估模式
    model.eval()

#     myLogger.info(f"模型加载成功")
# except:
#     myLogger.error(f"模型加载报错")





# 用作测试的环境状态




#########################





def pre_cdu_tso_fun(
        dryfan1=50,
        dryfan2=50,
        dryfan3=50,
        dryfan4=50,
        dryfan5=50,
        cduValve1=50,
        env_state_dict={'temperature': 27.9, 'cduTsi': 45.1, 'cduTfi': 29.4, 'cduTfo': 43.2, 'cduValve1': 28.0, 'dryFan1': 75.0, 'cduTso': 36.0},
        need_all_key=['temperature', 'cduTsi', 'cduTfi', 'cduTfo', 'cduValve1', 'dryFan1', 'cduTso']):
    '''
    :param dryfan1, dryfan2, dryfan3, dryfan4, dryfan5: 输入为int 整数类型，范围在 0~100之间
    :param cduValve1: 输入为int 整数类型，范围在 5~95之间
    :return: 当前动作配置下，输出的cduTso

    :状态字典: 仅仅需要字典类型，不需要转换为pddata类型
    '''
    new_all_sorted_dict = {}

    # 定义一个辅助函数来检查输入是否在 5 到 95 之间
    def check_range(value, name):
        if not (0 <= value <= 95):
            raise ValueError(f"{name} 必须在 5 到 95 之间，但收到了 {value}")

    # 检查所有 dryfan 参数是否在 5 到 95 之间
    check_range(dryfan1, 'dryfan1')
    check_range(dryfan2, 'dryfan2')
    check_range(dryfan3, 'dryfan3')
    check_range(dryfan4, 'dryfan4')
    check_range(dryfan5, 'dryfan5')

    # 检查 cduValve1 是否在 5 到 95 之间
    check_range(cduValve1, 'cduValve1')

    # 将输入参数转换为整数
    dryfan1 = int(dryfan1)
    dryfan2 = int(dryfan2)
    dryfan3 = int(dryfan3)
    dryfan4 = int(dryfan4)
    dryfan5 = int(dryfan5)
    cduValve1 = int(cduValve1)
    ###########################################################################################################


    for key in need_all_key:
        if key == 'dryFan1':
            new_all_sorted_dict[key] = dryfan1
        elif key == 'dryFan2':
            new_all_sorted_dict[key] = dryfan2
        elif key == 'dryFan3':
            new_all_sorted_dict[key] = dryfan3
        elif key == 'dryFan4':
            new_all_sorted_dict[key] = dryfan4
        elif key == 'dryFan5':
            new_all_sorted_dict[key] = dryfan5
        elif key == 'cduValve1':
            new_all_sorted_dict[key] = cduValve1
        else:
            new_all_sorted_dict[key] = env_state_dict[key]

    #myLogger.info(f"当前环境状态为:{new_all_sorted_dict}")
    new_all_sorted_dict.pop('cduTso')
    # 输入特征
    all_pddata = pd.Series(new_all_sorted_dict)
    all_pddata = list(all_pddata)

    # 转换为张量
    xx = torch.tensor(all_pddata, dtype=torch.float, requires_grad=True).to(device)
    with torch.no_grad():
        pre_cdu_tso = model(xx)
        pre_cdu_tso_not_tensor = pre_cdu_tso.item()

    # # 保存预测的结果
    # deque_save_pre_cduTso.append(pre_cdu_tso_not_tensor)
    #
    return pre_cdu_tso_not_tensor

    # 使用加载的模型进行预测

# if __name__ == '__main__':
#     while(True):
#         pre_cdu_tso_fun()

